import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fucius/Models/talentModel.dart';

final CollectionReference talentCollection = Firestore.instance.collection('talent');

class FirestoreService {

  Future<Map> createTalent(String id, Map data) async {
    data['Name'] = data["First Name"] + " " + data["Middle Name"] +" " + data["Last Name"];
    final TransactionHandler createTransaction = (Transaction tx) async {
      final DocumentSnapshot ds = await tx.get(talentCollection.document(id));

      await tx.set(ds.reference, data);
      return data;
    };

    return Firestore.instance.runTransaction(createTransaction).then((data) {
      var map=new Map<String,dynamic>();
      map['First Name'] = data['First Name'];
      map['Middle Name'] = data['Middle Name'];
      map['Last Name'] = data['Last Name'];
      String a  = data["First Name"];
      String b = data['Middle Name'];
      String c = data['Last Name'];
      String n  = a + " " +b +  " " + c;
      map['Name'] = n;
      print(n);
      map['Job Title'] = data["Job Title"];
      map['Country'] = data['Country'];
      map['Major'] = data["Major"];
      map['Organization'] = data["Organization"];
      map['Phone'] = data["Phone"];
      map['Email'] = data['Email'];
      map['Personal Website'] = data['Personal Website'];
      map['Short Bio'] = data['Short Bio'];
      return map;
    }).catchError((error) {
      print('error: $error');
      return null;
    });
  }

  createJob(Map data) {
    Firestore.instance.collection('job').document(data["Id"]).setData({
      'Id': data["Id"],
      'Job Title': data["Job Title"],
      'Department': data["Department"],
      'College': data["College"],
      'University': data["University"],
      'Topic/Class/Title': data["Topic/Class/Title"],
      'Audenice': data["Audenice"],
      'Contacts': data["Contacts"],
      'Expect Time': data["Expect Time"],
      'Date': data['Date']
    });
  }




  Stream<QuerySnapshot> getTaskList({int offset, int limit}) {
    Stream<QuerySnapshot> snapshots = talentCollection.snapshots();

    if (offset != null) {
      snapshots = snapshots.skip(offset);
    }
    if (limit != null) {
      snapshots = snapshots.take(limit);
    }
    return snapshots;
  }
}



class Task{
  String _taskname;
  String _taskdetails;
  String _taskdate;
  String _tasktime;
  String _tasktype;

  Task(this._taskname,this._taskdetails,this._taskdate,this._tasktime,this._tasktype);

  Task.map(dynamic obj){
    this._taskname = obj['taskname'];
    this._taskdetails = obj['taskdetails'];
    this._taskdate = obj['taskdate'];
    this._tasktime = obj['tasktime'];
    this._tasktype = obj['tasktype'];
  }

  String get  taskname=> _taskname;
  String get taskdetails => _taskdetails;
  String get taskdate => _taskdate;
  String get tasktime => _tasktime;
  String get tasktype => _tasktype;

  Map<String,dynamic> toMap(){
    var map=new Map<String,dynamic>();
    map['taskname']=_taskname;
    map['taskdetails'] = _taskdetails;
    map['taskdate'] = _taskdate;
    map['tasktime'] = _tasktime;
    map['tasktype'] = _tasktype;
    return map;
  }

  Task.fromMap(Map<String,dynamic> map){
    this._taskname= map['taskname'];
    this._taskdetails = map['taskdetails'];
    this._taskdate = map['taskdate'];
    this._tasktime = map['tasktime'];
    this._tasktype = map['tasktype'];
  }
}